package com.gitee.zycra.union.api.manager;

import com.gitee.zycra.union.api.common.model.ApiProjectGroup;
import com.gitee.zycra.union.api.common.util.IdGenerator;
import com.gitee.zycra.union.api.common.vo.ProjectGroupVO;
import com.gitee.zycra.union.api.dao.ApiProjectDao;
import com.gitee.zycra.union.api.dao.ApiProjectGroupDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author zycra
 */
@Component
public class ApiProjectGroupManager {

    @Autowired
    private ApiProjectGroupDao apiProjectGroupDao;

    @Autowired
    private ApiProjectDao apiProjectDao;

    @Autowired
    private IdGenerator idGenerator;

    /**
     * 添加项目组
     *
     * @param groupName 项目组名称
     * @return 添加条数
     */
    public int addGroup(String groupName) {
        return apiProjectGroupDao.insert(new ApiProjectGroup().setGroupName(groupName).setId(idGenerator.nextId()));
    }

    /**
     * 根据ID更新项目组
     *
     * @param id        项目组ID
     * @param groupName 项目组名称
     * @return 更新条数
     */
    public int updateGroup(Long id, String groupName) {
        return apiProjectGroupDao.updateById(new ApiProjectGroup().setId(id).setGroupName(groupName));
    }

    /**
     * 根据ID删除项目组
     *
     * @param id 项目组ID
     * @return 删除条数
     */
    public int deleteGroup(Long id) {
        return apiProjectGroupDao.updateById(new ApiProjectGroup().setId(id).setDeleted(true));
    }

    /**
     * 查询所有组
     *
     * @return 所有组
     */
    public List<ProjectGroupVO> selectAllGroup() {
        return apiProjectGroupDao.selectAllGroup();
    }
}
